{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0",
   "metadata": {},
   "outputs": [],
   "source": [
    "#Ryan Burgert 2024\n",
    "\n",
    "#Setup:\n",
    "#    Run this in a Jupyter Notebook on a computer with at least one GPU\n",
    "#        `sudo apt install ffmpeg git`\n",
    "#        `pip install rp`\n",
    "#    The first time you run this it might be a bit slow (it will download necessary models)\n",
    "#    The `rp` package will take care of installing the rest of the python packages for you\n",
    "\n",
    "import rp\n",
    "\n",
    "rp.git_import('CommonSource') #If missing, installs code from https://github.com/RyannDaGreat/CommonSource\n",
    "import rp.git.CommonSource.noise_warp as nw\n",
    "\n",
    "FRAME = 2**-1 #We immediately resize the input frames by this factor, before calculating optical flow\n",
    "              #The flow is calulated at (input size) × FRAME resolution.\n",
    "              #Higher FLOW values result in slower optical flow calculation and higher intermediate noise resolution\n",
    "              #Larger is not always better - watch the preview in Jupyter to see if it looks good!\n",
    "FLOW = 2**4   #Then, we use bilinear interpolation to upscale the flow by this factor\n",
    "              #We warp the noise at (input size) × FRAME × FLOW resolution\n",
    "              #The noise is then downsampled back to (input size)\n",
    "              #Higher FLOW values result in more temporally consistent noise warping at the cost of higher VRAM usage and slower inference time\n",
    "LATENT = 8    #We further downsample the outputs by this amount - because 8 pixels wide corresponds to one latent wide in Stable Diffusion\n",
    "              #The final output size is (input size) ÷ LATENT regardless of FRAME and FLOW\n",
    "\n",
    "LATENT = 2    #Uncomment this line for a prettier visualization! But for Marigold, use LATENT=8\n",
    "\n",
    "\n",
    "#Here are some Eyeline examples! Uncomment one\n",
    "images = \"/fsx_scanline/from_eyeline/users/ahmet/siggraph_asia_2024_videos/source_data/KW/camera_tt_360/dynamic_0007/cam_*_color.png\"\n",
    "# images = \"/fsx_scanline/from_eyeline/users/ahmet/siggraph_asia_2024_videos/source_data/KW/three_quarters_static/dynamic_0007/cam_*_color.png\"\n",
    "# images = \"/efs/users/ryan.burgert/public/sharing/kevin_matte_video_Jun28_2024/rgb_on_white/*png\"\n",
    "\n",
    "#You can also use video files or URLs\n",
    "images = \"https://www.shutterstock.com/shutterstock/videos/1100085499/preview/stock-footage-bremen-germany-october-old-style-carousel-moving-on-square-in-city-horses-on-traditional.webm\"\n",
    "\n",
    "output_folder = rp.get_folder_name(rp.get_parent_folder(images))\n",
    "\n",
    "#See this function's docstring for more information!\n",
    "output = nw.get_noise_from_video(\n",
    "    images,\n",
    "    remove_background=False, #Set this to True to matte the foreground - and force the background to have no flow\n",
    "    save_files=True,         #Set this to False if you just want the noises without saving to a numpy file\n",
    "    visualize=True,          #Generates nice visualization videos and previews in Jupyter notebook\n",
    "    visualize_flow_sensitivity = .05,\n",
    "    \n",
    "    noise_channels=4,\n",
    "    output_folder=output_folder,\n",
    "    resize_frames=FRAME,\n",
    "    resize_flow=FLOW,\n",
    "    downscale_factor=round(FRAME * FLOW) * LATENT,\n",
    ");\n",
    "\n",
    "print(\"Noise shape:\"  ,output.numpy_noises.shape)\n",
    "print(\"Flow shape:\"   ,output.numpy_flows .shape)\n",
    "print(\"Output folder:\",output.output_folder)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python vaetuner",
   "language": "python",
   "name": "vaetuner"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
